/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : pop_certify.js
*  Description  : 실명인증 
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-05-08  김효순    Created.  
*  2012-05-08  김효순    Complete.  
*******************************************************************************/

jQuery(function($){
	// 주민번호
	function clsJumin()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOVALUE1": "주민번호를 입력해 주십시요."
			, "_NOVALUE2": "주민번호를 입력해 주십시요."
			, "_NOVALID": "정확한 주민번호를 입력해 주십시요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsJumin.prototype = {
		_init: function(){
			var $layer = $("div.pw_area01 div.pop_certify tr:eq(0)");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.find("input:text").attr("maxlength", 6);
			$layer.find("input:password").attr("maxlength", 7);
		}
		, isValid: function(){
			var jumin1 = this._HTMLDom["$layer"].find("input:text").val();
			var jumin2 = this._HTMLDom["$layer"].find("input:password").val();
			
			this.errMsg = null;
			
			if(!jumin1){
				this.errMsg = "_NOVALUE1";
				return false;
			}else if(!jumin2){
				this.errMsg = "_NOVALUE2";
				return false;
			}else if(!this.checkJumin()){
				this.errMsg = "_NOVALID";
				return false;
			}
			
			return true;
		}
		, getError: function(){
			if(this.errMsg) return this._ERRMSG[this.errMsg]
			else return null;
		}
		, checkJumin: function(){
			var ssn1 = this._HTMLDom["$layer"].find("input:text").val();
			var ssn2 = this._HTMLDom["$layer"].find("input:password").val();
			
			if(ssn1.length != 6 || ssn2.length != 7) return false;
			if(/\D/.test(ssn1) || /\D/.test(ssn2)) return false;
			var ssn=ssn1+ssn2;
			var a = new Array(13);
			for (var i=0; i < 13; i++) {
				a[i] = parseInt(ssn.charAt(i));
			}
			var k = 11 - (((a[0] * 2) + (a[1] * 3) + (a[2] * 4) + (a[3] * 5) + (a[4] * 6) + (a[5] * 7) + (a[6] * 8) + (a[7] * 9) + (a[8] * 2) + (a[9] * 3) + (a[10] * 4) + (a[11] * 5)) % 11);
			if (k > 9){
				k -= 10;
			}
			if (k != a[12]){
				return false;
			}
			return true;
		}
		, focus: function(){
			var $layer = this._HTMLDom["$layer"];
			
			switch(this.errMsg){
				case "_NOVALUE2":
					$layer.find("input:password").trigger("focus");
					break;
				default:
					$layer.find("input:text").trigger("focus");
					break;
			}
		}
		, getValue: function(){
			var jumin1 = this._HTMLDom["$layer"].find("input:text").val();
			var jumin2 = this._HTMLDom["$layer"].find("input:password").val();
			
			return jumin1 + jumin2;
		}
	}
	
	// 휴대폰 인증
	function clsPhone()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"_NOVALUE1": "휴대폰 번호를 입력해 주십시요."
			, "_NOVALUE2": "휴대폰 번호를 입력해 주십시요."
		}
		this.errMsg = null;
		
		this._init();
	}
	
	clsPhone.prototype = {
		_init: function(){
			var $layer = $("div.pw_area01 div.pop_certify tr:eq(1)");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.find("input:text:eq(0)").attr("maxlength", 4);
			$layer.find("input:text:eq(1)").attr("maxlength", 4);
		}
		, isValid: function(){
			var $layer = this._HTMLDom["$layer"];
			var val1 = $layer.find(":text:eq(0)").val();
			var val2 = $layer.find(":text:eq(1)").val();
			
			this.errMsg = null;
			if(!val1){
				this.errMsg = "_NOVALUE1";
				return false;
			}else if(!val2){
				this.errMsg = "_NOVALUE2";
				return false;
			}
			
			return true;
		}
		, getError: function(){
			if(this.errMsg) return this._ERRMSG[this.errMsg]
			else return null;
		}
		, focus: function(){
			var $layer = this._HTMLDom["$layer"];
			
			switch(this.errMsg){
				case "_NOVALUE2":
					$layer.find(":text:eq(1)").trigger("focus");
					break;
				default:
					$layer.find(":text:eq(0)").trigger("focus");
					break;
			}
		}
		, getValue: function(){
			var $layer = this._HTMLDom["$layer"];
			var val1 = $layer.find("select option:selected").text();
			var val2 = $layer.find(":text:eq(0)").val();
			var val3 = $layer.find(":text:eq(1)").val();
			
			return val1 + val2 + val3;
		}
	}
	
	function clsComm()
	{
		// 인증여부
		this.certInfo = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			this.certInfo = {
				"인증여부": null
			}
		}
		, getCertInfo: function(){
			var thisClass = this;
			
			//ajax
			setTimeout(function(){
				var rnd = document.objUtil.random(0, 1);
				
				thisClass.certInfo = {
					"인증여부": rnd
				}
				
				document.objControl.finishCertInfo();
			}, 1000);
		}
		, getCertData: function(){
			return this.certInfo;
		}
		, certSubmit: function(arg){
			var thisClass = this;
			
//			arg = {
//				"주민번호": null
//				, "휴대폰번호": null
//				, "강제진행": null
//			}

			//ajax
			setTimeout(function(){
				var rnd = document.objUtil.random(0, 2);
				var ret = {
					"결과코드": rnd
					, "메시지": null
				}
				
				document.objControl.finishCertSubmit(ret);
			}, 1000);
		}
	}
	
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objJumin = null;
		this.objPhone = null;
		
		this.reserveKey = null;
		this.actionType = null;
		
		this._init();
	}
	
	clsControl.prototype = {
		_init: function(){
			this.actionType = document.objUtil.getParam("q_type");
			if(!this.actionType) self.close();
			
			if(!opener) self.close();
			if(!opener.document.objControl) self.close();
			
			this.reserveKey = opener.document.objControl.getReserveKey();
			
			this.objComm = new clsComm();
			this.objJumin = new clsJumin();
			this.objPhone = new clsPhone();
			
			this._bind();
			
			$("div.pw_area01").find("p:eq(0), div.pop_certify").hide();
			$("div.pw_area01>p:eq(1)").hide();
			
			// 인증여부 가져오기
			document.objGlobal.spinStart("receiving...");
			this.objComm.getCertInfo();
		}
		, _bind: function(){
			var thisClass = this;
			
			// 확인
			$("div.pw_area01 img[alt=확인]").click(function(){
				var bCert = thisClass.objComm.getCertData()["인증여부"];
				
				if(bCert){
					if(opener){
						if(opener.document.objControl){
							opener.document.objControl.certified(window, thisClass.actionType, thisClass.reserveKey);
						}
					}
					
					self.close();
				}else{
					if(!thisClass.objJumin.isValid()){
						alert(thisClass.objJumin.getError());
						thisClass.objJumin.focus();
						return;
					}else if(!thisClass.objPhone.isValid()){
						alert(thisClass.objPhone.getError());
						thisClass.objPhone.focus();
						return;
					}
					
					document.objGlobal.spinStart("sending...");
					thisClass.objComm.certSubmit({
						"주민번호": thisClass.objJumin.getValue()
						, "핸드폰번호": thisClass.objPhone.getValue()
						, "강제진행": null
					});
				}
			});
			
			// 닫기
			$("img[alt='close']").click(function(){
				self.close();
			});
		}
		, certified: function(){
			$("div.pw_area01").find("p:eq(0), div.pop_certify").hide();
			$("div.pw_area01>p:eq(1)").show();
			
			document.objUtil.setWindowSize();
		}
		, nocertified: function(){
			$("div.pw_area01").find("p:eq(0), div.pop_certify").show();
			$("div.pw_area01>p:eq(1)").hide();
			
			document.objUtil.setWindowSize();
		}
		// 인증 여부 완료
		, finishCertInfo: function(){
			var thisData = this.objComm.getCertData();
				
			document.objGlobal.spinEnd();
			if(thisData["인증여부"]){
				opener.document.objControl.certified(window, this.actionType, this.reserveKey);
			}else{
				this.nocertified();
			}
		}
		// 인증 처리 완료
		, finishCertSubmit: function(ret){
			document.objGlobal.spinEnd();
			
			switch(ret["결과코드"]){
				case 0:
					alert("인증처리가 실패하였습니다.");
					break;
				case 1:
					this.certified();
					break;
				case 2:
					if(confirm("해당 휴대폰명의로 이미 인증된 회원정보가 있습니다.  계속 진행할경우 기존 인증된 회원은 미인증된 회원으로 변경됩니다.\n또한 기존에 해당 휴대폰번호로 인증한 회원정보로 참여중인 부킹예약 및 참여상태도 자동 취소 됩니다.\n계속 진행 하시겠습니까?")){
						document.objGlobal.spinStart("sending...");
						this.objComm.certSubmit({
							"주민번호": this.objJumin.getValue()
							, "핸드폰번호": this.objPhone.getValue()
							, "강제진행": true
						});
					}
					break;
			}
		}
	}
	
	document.objControl = new clsControl();
});